﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;

namespace LightSwitchApplication
{
    public partial class CreateNewVisit
    {
        partial void CreateNewVisit_InitializeDataWorkspace(global::System.Collections.Generic.List<global::Microsoft.LightSwitch.IDataService> saveChangesTo)
        {
            // Write your code here.
            this.VisitProperty = new Visit();
            this.VisitProperty.VisitGoalAchieved = null;

            if (this.ShopId.HasValue)
            {
                this.VisitProperty.VisitShop = this.DataWorkspace.ApplicationData.Shops.Where(e => e.Id == this.ShopId).FirstOrDefault();
                this.FindControl("VisitShop").IsReadOnly = true;
            }

            if (this.Application.User.HasPermission(Permissions.CanEditVisit))//admin
            {
            }
            else if (this.Application.User.HasPermission(Permissions.CanManageMyVisit))//sales person
            {
                string userName = this.Application.User.FullName;
                //for some reason LINQ does not work with external datasource link
                //var currentSalesPerson = this.DataWorkspace.ApplicationData.SalesPersons.Where(s => s.aspnet_Profile.PropertyValuesString == userName).FirstOrDefault();
                //if (currentSalesPerson != null)
                //{
                //    this.SalesPersonId = currentSalesPerson.Id;
                //    this.VisitProperty.VisitSalesPerson = currentSalesPerson;
                //    this.FindControl("VisitSalesPerson").IsReadOnly = true;
                //}

                foreach (SalesPerson item in this.DataWorkspace.ApplicationData.SalesPersons)
                {
                    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                    {
                        //this.SalesPersonId = item.Id;
                        this.VisitProperty.VisitSalesPerson = this.DataWorkspace.ApplicationData.SalesPersons.Where(e => e.Id == item.Id).FirstOrDefault();
                        this.FindControl("VisitSalesPerson").IsReadOnly = true;
                        break;
                    }
                }
            }
        }

        partial void CreateNewVisit_Saved()
        {
            // Write your code here.
            this.Close(false);
            Application.Current.ShowDefaultScreen(this.VisitProperty);
        }

        partial void Cancel_Execute()
        {
            this.Close(false);
        }

        partial void CreateNewVisit_Activated()
        {
            try
            {
                this.FindControl("Save").IsEnabled = (this.Application.User.HasPermission(Permissions.CanEditVisit)
                        || this.Application.User.HasPermission(Permissions.CanManageMyVisit)
                    );
            }
            catch (Exception)
            {
            }
        }
    }
}